package chances.epg.ui.taglib.template;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.jsp.PageContext;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.log.CommonsLogLogChute;
import org.apache.velocity.tools.view.ViewToolContext;
import org.apache.velocity.tools.view.jsp.JspToolContext;

import chances.epg.ui.taglib.html.EpgBodyTagSupport;

public class TemplateFactory {
	static TemplateFactory instance;

	private VelocityEngine velocityEngine;

	public final static String name = "TEMPLATE_ENGINE";

	public static TemplateFactory getInstance(ServletContext context) {

		TemplateFactory factory = (TemplateFactory) context.getAttribute(name);
		if (factory == null) {
			//String loader = context.getInitParameter("tag.vm.loader");
			String loader ="file";
			Properties props = new Properties();

			if ("file".equals(loader)) {
				String path = context.getRealPath("/WEB-INF/template/epg_ui");
				props.put(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path);
			}
			factory = new TemplateFactory(props);
			context.setAttribute(name, factory);
		}
		return factory;
	}

	protected TemplateFactory(Properties props) {
		velocityEngine = new VelocityEngine();
		velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,
				new CommonsLogLogChute());
		velocityEngine.setProperty(RuntimeConstants.ENCODING_DEFAULT, "UTF-8");
		velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
				"true");

		for (Entry<Object, Object> ele : props.entrySet()) {
			velocityEngine.setProperty(ele.getKey().toString(), ele.getValue());
		}
	}
	public String merge(Map<String, Object> params,String js) {
		String name = "js/"+js+".vm";
		VelocityContext  context = new VelocityContext();
		for (Map.Entry<String, Object> entry : params.entrySet()) {
			context.put(entry.getKey(),entry.getValue());
		}
		Template template = velocityEngine.getTemplate(name);
		StringWriter writer = new StringWriter();
		template.merge(context, writer);
		return writer.toString();
	}
	public void merge(Map<String, Object> params, EpgBodyTagSupport tag) {
		String name = tag.getTemplate()+".vm";
		PageContext pageContext = tag.getEpgPageContext().getPageContext();
		ViewToolContext context = new JspToolContext(velocityEngine,
				pageContext);
		context.putAll(params);
		Template template = velocityEngine.getTemplate(name);
		template.merge(context, pageContext.getOut());
	}

	protected static VelocityEngine createWebVelocityEngine(
			ServletContext servletContext) throws IOException,
			VelocityException {
		VelocityEngine velocityEngine = new VelocityEngine();
		velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,
				new CommonsLogLogChute());
		velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
		velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
				"true");

		velocityEngine.setProperty(RuntimeConstants.ENCODING_DEFAULT, "UTF-8");
		velocityEngine.init();
		return velocityEngine;
	}
}
